home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP09.ZIP / CHAP09 / PATRON / PAGES.CPP < prev    next >
C/C++ Source or Header  |  1993-06-23  |  28KB  |  1,251 lines

  1. /*
  2.  * PAGES.CPP
  3.  * Modifications for Chapter 9
  4.  *
  5.  * Implementation of the CPages class.  See PAGEWIN.CPP for additional
  6.  * member functions.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18.  
  19. #include "patron.h"
  20.  
  21.  
  22. HWND        g_hDlgPrint=NULL;
  23. BOOL        g_fCancelPrint=FALSE;
  24.  
  25.  
  26. /*
  27.  * CPages:CPages
  28.  * CPages::~CPages
  29.  *
  30.  * Constructor Parameters:
  31.  *  hInst           HINSTANCE of the application we're in.
  32.  *  cf              UINT application clipboard format.
  33.  */
  34.  
  35. CPages::CPages(HINSTANCE hInst, UINT cf)
  36.     : CWindow(hInst)
  37.     {
  38.     m_pPageCur=NULL;
  39.     m_iPageCur=0xFFFF;  //Pages are 0 indexed, so this is one before that.
  40.     m_cPages=0;
  41.     m_hWndPageList=NULL;
  42.  
  43.     /*
  44.      * Initialize to 2.54cm*2.54cm which is a page with no space for anything,
  45.      * just margins.  2.54cm=.5 inches on each margin.
  46.      */
  47.     m_cx=LOMETRIC_PER_INCH;
  48.     m_cy=LOMETRIC_PER_INCH;
  49.  
  50.     m_xPos=0L;
  51.     m_yPos=0L;
  52.  
  53.     m_dwIDNext=0;
  54.     m_pIStorage=NULL;
  55.  
  56.     m_fDirty=FALSE;
  57.     m_cf=cf;
  58.  
  59.     m_fDragSource=FALSE;
  60.     m_fMoveInPage=FALSE;
  61.  
  62.     m_fDragRectShown=FALSE;
  63.  
  64.     m_uScrollInset=GetProfileInt("windows", "DragScrollInset", DD_DEFSCROLLINSET);
  65.     m_uScrollDelay=GetProfileInt("windows", "DragScrollDelay", DD_DEFSCROLLDELAY);
  66.  
  67.     m_uHScrollCode=0;
  68.     m_uVScrollCode=0;
  69.     return;
  70.     }
  71.  
  72.  
  73. CPages::~CPages(void)
  74.     {
  75.     //Insure memory is cleaned up in list, and do final IStorage::Release
  76.     FIStorageSet(NULL, FALSE, FALSE);
  77.  
  78.     if (NULL!=m_hFont && !m_fSystemFont)
  79.         DeleteObject(m_hFont);
  80.  
  81.     if (NULL!=m_hWndPageList)
  82.         DestroyWindow(m_hWndPageList);
  83.  
  84.     //m_hWnd destroyed with the document.
  85.     return;
  86.     }
  87.  
  88.  
  89.  
  90. /*
  91.  * CPages::FIsDirty
  92.  *
  93.  * Purpose:
  94.  *  Tells the caller (document) if anything's happened to dirty us.
  95.  *
  96.  * Parameters:
  97.  *  None
  98.  *
  99.  * Return Value:
  100.  *  None
  101.  */
  102.  
  103. BOOL CPages::FIsDirty(void)
  104.     {
  105.     return m_fDirty;
  106.     }
  107.  
  108.  
  109.  
  110. /*
  111.  * CPages::FInit
  112.  *
  113.  * Purpose:
  114.  *  Instantiates a pages window within a given parent.  The
  115.  *  parent may be a main application window, could be an MDI child
  116.  *  window. We really do not care.
  117.  *
  118.  * Parameters:
  119.  *  hWndParent      HWND of the parent of this window
  120.  *  pRect           LPRECT that this window should occupy
  121.  *  dwStyle         DWORD containing the window's style flags.  Should
  122.  *                  contain WS_CHILD | WS_VISIBLE in typical circumstances.
  123.  *  uID             UINT ID to associate with this window
  124.  *  pv              LPVOID unused for now.
  125.  *
  126.  * Return Value:
  127.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  128.  */
  129.  
  130. BOOL CPages::FInit(HWND hWndParent, LPRECT pRect, DWORD dwStyle
  131.     , UINT uID, LPVOID pv)
  132.     {
  133.     int     cy;
  134.  
  135.     m_hWnd=CreateWindowEx(WS_EX_NOPARENTNOTIFY, SZCLASSPAGES
  136.         , SZCLASSPAGES, dwStyle, pRect->left, pRect->top
  137.         , pRect->right-pRect->left, pRect->bottom-pRect->top
  138.         , hWndParent, (HMENU)uID, m_hInst, (LPVOID)this);
  139.  
  140.     if (NULL==m_hWnd)
  141.         return FALSE;
  142.  
  143.     /*
  144.      * Create the hidden listbox we'll use to track pages.  We give it
  145.      * the owner-draw style so we can just store pointers in it.  We have
  146.      * to set the parent to NULL such that the window hangs around after
  147.      * the pages window is destroyed so that we can clean up the
  148.      * memory stored in it from the CPages destructor.
  149.      */
  150.     m_hWndPageList=CreateWindow("listbox", "Page List", WS_POPUP | LBS_OWNERDRAWFIXED
  151.         , 0, 0, 100, 100, HWND_DESKTOP, NULL, m_hInst, NULL);
  152.  
  153.     if (NULL==m_hWndPageList)
  154.         return FALSE;
  155.  
  156.     //Create a 14 point Arial font, or use the system variable font.
  157.     cy=MulDiv(-14, LOMETRIC_PER_INCH, 72);
  158.     m_hFont=CreateFont(cy, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE
  159.         , ANSI_CHARSET, OUT_TT_PRECIS, CLIP_TT_ALWAYS, PROOF_QUALITY
  160.         , VARIABLE_PITCH | FF_SWISS, "Arial");
  161.  
  162.     if (NULL==m_hFont)
  163.         {
  164.         m_hFont=(HFONT)GetStockObject(ANSI_VAR_FONT);
  165.         m_fSystemFont=TRUE;
  166.         }
  167.  
  168.     return TRUE;
  169.     }
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. /*
  177.  * CPages::FIStorageSet
  178.  *
  179.  * Purpose:
  180.  *  Provides the document's IStorage to the pages for its own uses.
  181.  *  If this is a new storage, then we initalize it with streams we
  182.  *  want to always exists.  If this is an open, then we create
  183.  *  our page list from the PageList string we wrote before.
  184.  *
  185.  * Parameters:
  186.  *  pIStorage       LPSTORAGE to the new or opened storage.  If this is
  187.  *                  NULL then we just clean up and exit.
  188.  *  fChange         BOOL indicating is this was a Save As operation
  189.  *                  meaning that we have the structure already, we
  190.  *                  just need to change our value of m_pIStorage.
  191.  *  fInitNew        BOOL indicating if this is a new storage or one
  192.  *                  opened from a previous save.
  193.  *
  194.  * Return Value:
  195.  *  None
  196.  */
  197.  
  198. BOOL CPages::FIStorageSet(LPSTORAGE pIStorage, BOOL fChange, BOOL fInitNew)
  199.     {
  200.     DWORD           dwMode=STGM_DIRECT | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
  201.     HRESULT         hr;
  202.     LPPAGE          pPage;
  203.     BOOL            fRet=FALSE;
  204.     ULONG           cbRead;
  205.     PAGELIST        pgList;
  206.     LPSTREAM        pIStream;
  207.     LPMALLOC        pIMalloc;
  208.     LPDWORD         pdwID;
  209.     UINT            i;
  210.  
  211.     //If we're just changing saved roots, just open the current page again
  212.     if (fChange)
  213.         {
  214.         if (NULL==pIStorage)
  215.             return FALSE;
  216.  
  217.         m_pIStorage->Release();
  218.         m_pIStorage=pIStorage;
  219.         m_pIStorage->AddRef();
  220.  
  221.         FPageGet(m_iPageCur, &m_pPageCur, TRUE);
  222.         return TRUE;
  223.         }
  224.  
  225.     if (NULL!=m_hWndPageList)
  226.         {
  227.         //On new or open, clean out whatever it is we have.
  228.         for (i=0; i < m_cPages; i++)
  229.             {
  230.             if (FPageGet(i, &pPage, FALSE))
  231.                 delete pPage;
  232.             }
  233.  
  234.         SendMessage(m_hWndPageList, LB_RESETCONTENT, 0, 0L);
  235.         }
  236.  
  237.     if (NULL!=m_pIStorage)
  238.         m_pIStorage->Release();
  239.  
  240.     m_pIStorage=NULL;
  241.  
  242.     //If we're just cleaning up, then we're done.
  243.     if (NULL==pIStorage)
  244.         return TRUE;
  245.  
  246.     m_pIStorage=pIStorage;
  247.     m_pIStorage->AddRef();
  248.  
  249.     //If this is a new storage, create the streams we require
  250.     if (fInitNew)
  251.         {
  252.         //Page list header.
  253.         hr=m_pIStorage->CreateStream(SZSTREAMPAGELIST, dwMode | STGM_CREATE
  254.             , 0, 0, &pIStream);
  255.  
  256.         if (FAILED(hr))
  257.             return FALSE;
  258.  
  259.         pIStream->Release();
  260.  
  261.         //Device Configuration
  262.         hr=m_pIStorage->CreateStream(SZSTREAMDEVICECONFIG, dwMode | STGM_CREATE
  263.             , 0, 0, &pIStream);
  264.  
  265.         if (FAILED(hr))
  266.             return FALSE;
  267.  
  268.         pIStream->Release();
  269.         return TRUE;
  270.         }
  271.  
  272.  
  273.     /*
  274.      * We're opening an existing file:
  275.      *  1)  Configure for the device we're on
  276.      *  2)  Read the Page List and create page entries for each.
  277.      */
  278.  
  279.     ConfigureForDevice();
  280.  
  281.     //Read the page list.
  282.     hr=m_pIStorage->OpenStream(SZSTREAMPAGELIST, NULL, dwMode, 0, &pIStream);
  283.  
  284.     if (FAILED(hr))
  285.         return FALSE;
  286.  
  287.     if (SUCCEEDED(CoGetMalloc(MEMCTX_SHARED, &pIMalloc)))
  288.         {
  289.         pIStream->Read((LPVOID)&pgList, sizeof(PAGELIST), &cbRead);
  290.         m_cPages  =(UINT)pgList.cPages;
  291.         m_iPageCur=(UINT)pgList.iPageCur;
  292.         m_dwIDNext=pgList.dwIDNext;
  293.  
  294.         fRet=TRUE;
  295.         cbRead=pgList.cPages*sizeof(DWORD);
  296.  
  297.         if (0!=cbRead)
  298.             {
  299.             pdwID=(LPDWORD)pIMalloc->Alloc(cbRead);
  300.  
  301.             if (NULL!=pdwID)
  302.                 {
  303.                 pIStream->Read((LPVOID)pdwID, cbRead, &cbRead);
  304.  
  305.                 for (i=0; i < m_cPages; i++)
  306.                     fRet &=FPageAdd(-1, *(pdwID+i), FALSE); //-1==end of list
  307.  
  308.                 pIMalloc->Free((LPVOID)pdwID);
  309.                 }
  310.             }
  311.  
  312.